import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD, Adam

# 将一些网络层通过.add()堆叠起来，就构成了一个模型：

model = Sequential()
model.add(Dense(input_dim=28*28, output_dim=500))
# model.add(Activation("sigmoid"))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(output_dim=500))
# model.add(Activation("sigmoid"))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(output_dim=10))
model.add(Activation("softmax"))
# 完成模型的搭建后，我们需要使用.compile()方法来编译模型：
# model.compile(loss='mse', optimizer=SGD(lr=0.1), metrics=['accuracy'])    //0.6678成功率
# model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.1), metrics=['accuracy']) #更换丢失率函数 成功率立马上到 0.9419
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy']) #引入动量

# 装载一些数据    https://keras.io/datasets/
# from keras.datasets import mnist
# (x_train, y_train), (x_test, y_test) = mnist.load_data()

import numpy as np
num_classes = 10
f = np.load("./mnist.npz")
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
#完成模型编译后，我们在训练数据上按batch进行一定次数的迭代来训练网络
model.fit(x_train, y_train, batch_size=100, nb_epoch=20)

#随后，我们可以使用一行代码对我们的模型进行评估，看看模型的指标是否满足我们的要求：
score = model.evaluate(x_test, y_test)
print('Total loss on Testing Set:', score[0])
print('Accuracy on Testing Set:', score[1])
#或者，我们可以使用我们的模型，对新的数据进行预测：
# result = model.predict(x_test)

